import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:aihu_getx_app/utils/custom_get_util.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';

class SharePosterUtils {
  static Future<ByteData?> getWidgetToImageByteData(
      GlobalKey repaintKey) async {
//存储权限
    PermissionStatus status = await Permission.storage.status;
    LogUtil.v(status, tag: 'status');
    if (!status.isGranted) {
      LogUtil.v("获取存储权限");
      status = await Permission.storage.request();
      return null;
    }
    BuildContext? buildContext = repaintKey.currentContext;
    if (null != buildContext) {
      RenderRepaintBoundary? boundary =
          buildContext.findRenderObject() as RenderRepaintBoundary?;
      ui.Image? image = await boundary?.toImage(pixelRatio: 3.0);
      ByteData? byteData =
          await image?.toByteData(format: ui.ImageByteFormat.png);
      if (byteData == null) {
        EasyLoading.show(status: '保存失败, == >>> 获取 image.toByteData 失败');
        return null;
      } else {
        return byteData;
      }
    }
    return null;
  }

  static Future<String?> getImageFilePath(GlobalKey repaintKey) async {
    ByteData? byteData = await getWidgetToImageByteData(repaintKey);
    if (byteData == null) {
      LogUtil.e('获取 byteData 失败 ');
      return null;
    }
    Uint8List imageByte = byteData.buffer.asUint8List();
    var tempDir = await getTemporaryDirectory();
    var file =
        await File('${tempDir.path}/image_${DateTime.now().millisecond}.jpg')
            .create();
    file.writeAsBytesSync(imageByte);
    return path.normalize(file.path);
  }

  static void savePosterImage(GlobalKey repaintKey) async {
    ByteData? byteData = await getWidgetToImageByteData(repaintKey);
    if (byteData == null) {
      LogUtil.e('获取 byteData 失败 ');
      return;
    }
    final result =
        await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
    if (result['isSuccess']) {
      LogUtil.v('保存成功。。。');
      CustomGetUtil.snackbar('提示', '保存成功');
    } else {
      CustomGetUtil.snackbar('提示', '保存失败');
      LogUtil.e('操作失败。。。');
    }
  }

  //share_plus分享
  static void onSharePlusShare(GlobalKey repaintKey) async {
    //获取截图地址
    String? filePath = await getImageFilePath(repaintKey);
    if (filePath == null) {
      LogUtil.e('获取 filePath 失败 ');
      return;
    }
    final result =
        await Share.shareXFiles([XFile(filePath)], text: 'Great picture');

    if (result.status == ShareResultStatus.success) {
      print('Thank you for sharing the picture!');
    }
  }
}
